package com.jy.unkown.page;

import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.view.WindowManager;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.jiayou.apiad.dialog.LoadProgressDialog;

import java.io.File;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * zhangzl
 * 时间： 2020/7/1 11:17
 * 描述：
 */
public abstract class BUnkownActivity extends AppCompatActivity {

    public AppCompatActivity activity;
    private CompositeDisposable compositeDisposable = new CompositeDisposable();
    public void addDisposable(Disposable disposable) {
        compositeDisposable.add(disposable);
    }

    private LoadProgressDialog loadProgressDialog;
    public void showLoading() {
        try {
            if (loadProgressDialog == null) {
                loadProgressDialog = new LoadProgressDialog(activity);
            }
            if (!loadProgressDialog.isShowing()) {
                loadProgressDialog.show();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void hideLoading() {
        try {
            if (loadProgressDialog != null) {
                loadProgressDialog.dismiss();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public abstract int layoutId();
    public abstract void init();



    public int mAdTime = 0;
    public long totalTime = 0L;
    public long daojishiTime = 0L;
    /**
     * 是否已经播放完毕  包含正常播放完毕和播放异常
     */
    public boolean isPlayOver = false;

    public boolean isCanClick() {
        if (mAdTime < 0) {
            if (totalTime > 0) {
                return true;
            }
        } else if (mAdTime == 0) {
            if (totalTime > 0 && isPlayOver) {
                return true;
            }
        } else {
            if (totalTime > 0 && daojishiTime <= mAdTime) {
                return true;
            }
        }

        return totalTime > 0 && daojishiTime <= 0;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        activity = this;
        setContentView(layoutId());


        init();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        compositeDisposable.clear();
    }

    public void showToast(String msg) {
        if (!TextUtils.isEmpty(msg)) {
            try {
                Toast.makeText(activity, msg, Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public String absolutePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "Download" + File.separator;

    /**
     * 下载apk
     * @param url
     * @param title
     */
    public void downloadApk(String url, String title) {

    }
}
